package cn.juque.datapipeline.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.juque.common.base.BaseRequestVO;
import cn.juque.common.base.BaseResponseVO;
import cn.juque.common.base.PageInfo;
import cn.juque.common.constants.MessageEnum;
import cn.juque.datapipeline.bo.task.TaskAddBO;
import cn.juque.datapipeline.bo.task.TaskUpdateBO;
import cn.juque.datapipeline.converter.TaskInfoAddConverter;
import cn.juque.datapipeline.domain.TaskInfoDO;
import cn.juque.datapipeline.domain.TaskInfoSearchParamDO;
import cn.juque.datapipeline.entity.TaskInfo;
import cn.juque.datapipeline.service.ITaskInfoService;
import cn.juque.datapipeline.vo.taskinfo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>TaskInfoController</li>
 * </ul>
 * @date 2023-03-31 14:32:19
 **/
@Slf4j
@Controller
@RequestMapping("/dpp/taskInfo")
public class TaskInfoController extends BaseController {

    @Resource
    private ITaskInfoService taskInfoService;

    @Resource
    private TaskInfoAddConverter taskInfoAddConverter;

    /**
     * 查询
     *
     * @param request BaseRequestVO
     * @return BaseResponseVO
     */
    @PostMapping(value = "/list")
    @ResponseBody
    public BaseResponseVO<List<TaskInfoListVO>> list(@RequestBody BaseRequestVO<TaskInfoSearchVO> request) {
        TaskInfoSearchVO searchVO = super.validatorUtil.checkNotNull(request);
        PageInfo pageInfo = request.getPageInfo();
        super.validatorUtil.checkPageInfo(pageInfo);
        TaskInfoSearchParamDO searchParamDO = new TaskInfoSearchParamDO();
        BeanUtil.copyProperties(searchVO, searchParamDO);
        List<TaskInfoDO> list = this.taskInfoService.listInfoWithPage(pageInfo, searchParamDO);
        List<TaskInfoListVO> voList = BeanUtil.copyToList(list, TaskInfoListVO.class);
        return new BaseResponseVO<>(MessageEnum.OK, voList, pageInfo);
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return BaseResponseVO
     */
    @GetMapping(value = "/detail")
    @ResponseBody
    public BaseResponseVO<TaskInfoDetailVO> detail(String id) {
        Assert.notEmpty(id);
        TaskInfo detail = this.taskInfoService.detail(id);
        TaskInfoDetailVO detailVO = new TaskInfoDetailVO();
        BeanUtil.copyProperties(detail, detailVO);
        return new BaseResponseVO<>(MessageEnum.OK, detailVO);
    }

    /**
     * 新增
     *
     * @param request BaseRequestVO
     * @return BaseResponseVO
     */
    @PostMapping(value = "/add")
    @ResponseBody
    public BaseResponseVO<Boolean> add(@RequestBody BaseRequestVO<TaskInfoAddVO> request) {
        super.validatorUtil.fastValidate(request);
        TaskInfoAddVO addVO = request.getParam();
        TaskAddBO addBO = this.taskInfoAddConverter.toTaskAddBO(addVO);
        this.taskInfoService.saveInfo(addBO);
        return new BaseResponseVO<>(MessageEnum.OK, true);
    }

    /**
     * 更新
     *
     * @param request BaseRequestVO
     * @return BaseResponseVO
     */
    @PostMapping(value = "/edit")
    @ResponseBody
    public BaseResponseVO<Boolean> edit(@RequestBody BaseRequestVO<TaskInfoUpdateVO> request) {
        super.validatorUtil.fastValidate(request);
        TaskInfoUpdateVO updateVO = request.getParam();
        TaskUpdateBO updateBO = this.taskInfoAddConverter.toTaskUpdateBO(updateVO);
        this.taskInfoService.updateInfo(updateBO);
        return new BaseResponseVO<>(MessageEnum.OK, true);
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return BaseResponseVO
     */
    @GetMapping(value = "/triggerEnable")
    @ResponseBody
    public BaseResponseVO<Boolean> triggerEnable(String id) {
        Assert.notEmpty(id);
        this.taskInfoService.triggerEnable(id);
        return new BaseResponseVO<>(MessageEnum.OK, true);
    }

    /**
     * 删除
     *
     * @param request BaseRequestVO
     * @return BaseResponseVO
     */
    @PostMapping(value = "/delete")
    @ResponseBody
    public BaseResponseVO<Boolean> delete(@RequestBody BaseRequestVO<TaskInfoDeleteVO> request) {
        super.validatorUtil.fastValidate(request);
        List<String> idList = request.getParam().getIdList();
        this.taskInfoService.deleteInfo(idList);
        return new BaseResponseVO<>(MessageEnum.OK, true);
    }
}
